﻿/**
 * Jean-François Desgagné 2009 Portfolio
 * Copyright (c) 2009 Jean-François Desgagné <jfesgagne@gmail.com>
 * 
 * This work is licensed under a Creative Commons Attribution 3.0 United States License.
 * Some Rights Reserved.
 */
package com.jfdesgagne.jfdesgagne09.shell.view {
    import com.jfdesgagne.jfdesgagne09.shell.ApplicationFacade;
	import com.jfdesgagne.jfdesgagne09.shell.model.LocaleProxy;
	import com.jfdesgagne.jfdesgagne09.shell.view.component.Tooltip;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.display.DisplayObject;
	
    import org.puremvc.as3.multicore.interfaces.*;
    import org.puremvc.as3.multicore.patterns.mediator.Mediator;
    
    public class TooltipMediator extends Mediator implements IMediator {
        // Cannonical name of the Mediator
        public static const NAME:String = "TooltipMediator";
		private var _timer:Timer;
        private var _info:Object;
		
        public function TooltipMediator(viewComponent:Object) {
            super(NAME, viewComponent);
			
			this.tooltip.tooltip_mc.drawning_txt.text = LocaleProxy.getString("IDS_TOOLTIP_DRAWNING");
			this.tooltip.tooltip_mc.keyboard_txt.text = LocaleProxy.getString("IDS_TOOLTIP_KEYBOARD");
			this._timer = new Timer(1000, 1);
			this._timer.addEventListener(TimerEvent.TIMER_COMPLETE, this._timerCompleteHandler);
        }
		
		private function _timerCompleteHandler(event:TimerEvent):void {
			this._show();
		}
		
		private function _show():void {
			this._timer.stop();
			if(!StageMediator.getInstance().stage.contains(this.tooltip as DisplayObject)) StageMediator.getInstance().stage.addChild(this.tooltip);
			this.tooltip.show(this._info.clip, this._info.letter, this._info.text);
		}
		
		private function _hide():void {
			this._timer.stop();
			if(StageMediator.getInstance().stage.contains(this.tooltip as DisplayObject)) StageMediator.getInstance().stage.removeChild(this.tooltip);
		}

		override public function listNotificationInterests():Array {
            return [ 
					ApplicationFacade.SHOW_TOOLTIP,
            		ApplicationFacade.HIDE_TOOLTIP
                   ];
        }

        override public function handleNotification(note:INotification):void {
        	switch (note.getName()) {
                case ApplicationFacade.SHOW_TOOLTIP:
					this._info = note.getBody();
					this._timer.reset();
					this._timer.start();
				break;
				
				case ApplicationFacade.HIDE_TOOLTIP:
					this._hide();
				break;
            }
        }
		
		protected function get tooltip():Tooltip {
            return this.viewComponent as Tooltip;
        }
    }
}